﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Net;
using System.IO;
using System.Net.Sockets;
using System.Threading;
using System.Net;

using System.Management;
using System.Runtime.InteropServices;

namespace NetChatClient
{
    public class SocketConnection
    {   
        //变量

        //服务器接收端
        public Socket s;         //初始化的套接字
        public Socket CSocket;    //连接服务器的socket
        public NetworkStream ns;//网络流
        public StreamReader sr; //读入流
        public StreamWriter sw; //写入流

        //客户端
        public Socket s1;
        public NetworkStream ns1;
        public StreamReader sr1;
        public StreamWriter sw1;



        SocketConnection(Socket s)//套接字初始化
        {
            //Do Initialization
             s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);  //use protocal TCP
        }
        public IPAddress BindLocateIP(/*IP Address*/)//绑定到本地IP地址
        {
            String hostname;
            hostname = Dns.GetHostName();
            IPAddress ServerIp = Dns.Resolve(hostname).AddressList[0];
            return ServerIp;

        }

        public string[] getRemoteIP(string RemoteHostName)   //获取远程主机IP
       {
        IPHostEntry ipEntry = Dns.GetHostByName(RemoteHostName);
        IPAddress[] IpAddr = ipEntry.AddressList;
        string[] strAddr = new string[IpAddr.Length];
        for (int i=0;i<IpAddr.Length;i++)
        {
        strAddr[i] = IpAddr[i].ToString();
        }
        return(strAddr);
        }

        public string getRemoteMac(string localIP, string remoteIP)  //获取远程主机MAC
        {
            Int32 ldest = inet_addr(remoteIP); //目的ip 
            Int32 lhost = inet_addr(localIP); //本地ip 

            try
            {
                Int64 macinfo = new Int64();
                Int32 len = 6;
                int res = SendARP(ldest, 0, ref macinfo, ref len);
                return Convert.ToString(macinfo, 16);
            }
            catch (Exception err)
            {
                Console.WriteLine("Error:{0}", err.Message);
            }
            return 0.ToString();
        } 

        public void BindPort(Socket s,uint port,int numlisten)//绑定端口,并进入监听。"numlisten"表示监听数量。
        {
            
            IPEndPoint server = new IPEndPoint(BindLocateIP(), port);  //绑定"port"号端口。
            s.Bind(server);
            s.Listen(numlisten);

        }
        public void SetUsePort(uint port)//一个程序可以有多个端口,但是当前信息发送出去选择哪个端口发送
        {

        }
        public void ConnectTo(Socket s,Socket CSocket)   //作为服务器端的连接
        {
            while (true)
            {
                try
                {
                    CSocket = s.Accept();
                    if (CSocket.Connected)   //
                    {
                        //If connect ,operator here
                    }
                    else
                    {

                        MessageBox.Show("连接失败！");
                    }

                }
                catch (SocketException s)
                {
                    MessageBox.Show(s.Message);
                }
                catch (IOException io)
                {
                    MessageBox.Show(io.Message);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("其他异常" + ex.Message);
                }
            }
        }
        public void SendMsg(Socket s,NetworkStream ns1,StreamReader sr1,StreamWriter sw1,string Msg,uint port, System.Windows.Forms.ListBox lbInfo)//发送文字消息
        {
            SocketConnection(s);
            try
            {
                s.Connect(BindLocateIP(), port);/////////////port此处可不可以改为SetUsePort

            }
            catch (Exception ex)
            {
                MessageBox.Show("message error:" + ex.Message);
            }
            try
            {
                ns1 = new NetworkStream(s);
                sr1 = new StreamReader(ns1);
                sw1 = new StreamWriter(ns1);
                lbInfo.Items.Add(sr1.ReadLine());
                //sr1.ReadLine();
                sw1.WriteLine(Msg);
                sw1.Flush();
             }
            catch (Exception ei)
            {
                MessageBox.Show("message error" + ei.Message);
            }
        }
        public void SendMsg(Point point)
        {

        }
        public void SendMsg(int n)
        {

        }
        public void RecvMsg(Socket s1, NetworkStream ns, StreamReader sr, StreamWriter sw, ref string Msg,uint port,int numlisten,System.Windows.Forms.ListBox lbInfo)//接受文字消息
        {
            SocketConnection(s1);
            BindPort(s1, port,numlisten);
            ns = new NetworkStream(s1);
            sr = new StreamReader(ns);
            sw = new StreamWriter(ns);
            lbInfo.Items.Add(Msg);
            lbInfo.Items.Add(sr1.ReadLine());
            sw.WriteLine(Msg);
            sw.Flush();
        }
        public void RecvMsg(ref Point point)
        {

        }
        public void RecvMsg(ref int n)
        {

        }
        public void SendObject(object obj)//发送对象,如图片等
        {

        }
        public void RecvObject(object obj)//接受对象,如图片等
        {

        }
    }
    
}
